package com.letou.ls.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.table.TableTitle;

import com.letou.ls.R;
import com.letou.ls.mode.LotteryInfo;
import com.letou.ls.uitl.Constant;
import com.letou.ls.uitl.Util;
import com.letou.ls.view.MLinearLayout;
import com.letou.ls.view.TableBadyView;
import com.letou.ls.view.TableFifFiveBottmBady;
import com.letou.ls.view.TableFifFiveTitleView;
import com.letou.ls.view.TableViewUtil;
import com.letou.ls.view.VerticalTextView;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * 黑龙江快乐十分麻将 横
 *
 * @author Administrator
 *
 */
public class HappyMajiangActivity extends BaseActivity {
    private LinearLayout mTopLayout;
    private LinearLayout mBottomLayout;
    private LinearLayout mContextLayout;
    private LinearLayout lLayout;
    private TableFifFiveTitleView mTableTopTitle;
    private TableBadyView mTableBadyView;
    private TableFifFiveBottmBady mTableBottmBady;
    private ScrollView scrollview;
    
    float textSize;
    private float itemHeight;
    private ArrayList<Float> widthList;
    private float density;
    private Button menuBtn;
    private LinearLayout menuParent;
    private TextView startLotto;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 添加 标记
        lotteryCode = Constant.LOTTERY_CODE_03;
        screen_direction_type = 1;
        // lotteryCode = "hljklsfmj";
        initFirstDateTime("8:44:59", "8:55:00");
        setContentView(R.layout.activity_happy_majiang);
        loadHorizontalLayout();
        //
        // if (dataHor == null) {
        // dataHor = loadDataHor();
        // }
        //
        // if (bottomdataHor == null) {
        // bottomdataHor = getBottomdataHor();
        // }
        // mTableBadyView.updateData(dataHor, widthList, textSize * density,
        // itemHeight);
        //
        // // ArrayList<Float> list =
        // // TableViewUtil.getBottomWidthForMajiang(widthList);
        // ArrayList<Float> list = TableViewUtil.getBottomWidth(widthList, 4,
        // 24, 25);
        // mTableBottmBady.addData(bottomdataHor, list, textSize,
        // R.drawable.icon_happy10_majiang, 21);
        
    }
    
    private void loadHorizontalLayout() {
        mTopLayout = (LinearLayout) findViewById(R.id.majiang_top_layout);
        mContextLayout = (LinearLayout) findViewById(R.id.majiang_context_layout);
        mBottomLayout = (LinearLayout) findViewById(R.id.majiang_bottom_layout);
        lLayout = (LinearLayout) findViewById(R.id.lLayout);
        scrollview = (ScrollView) findViewById(R.id.majiang_scrollview);
        menuParent = (LinearLayout) findViewById(R.id.majiang_menu_parent);
        menuBtn = (Button) findViewById(R.id.majiang_menu_bt);
        startLotto = (TextView) findViewById(R.id.startLotto);
        
        // VerticalListTextView vTop = (VerticalListTextView) findViewById(R.id.vTextTop);
        VerticalTextView vTop = (VerticalTextView) findViewById(R.id.vTextTop);
        String t1 = getResources().getString(R.string.tip_top);
        String t2 = getResources().getString(R.string.tip_bottom);
        String space = "  ";
        
        // VerticalTextView vBottom = (VerticalTextView) findViewById(R.id.vTextBottom);
        // vBottom.setText(getResources().getString(R.string.tip_bottom));
        
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        
        if (metric.widthPixels == 1920) vTop.setTextSize(40);
        vTop.setText(t1 + space + t2 + space + Util.getDataLine(this) + space);
        
        density = metric.density; // 屏幕密度（0.75 / 1.0 / 1.5）
        int viewWidth = 0;
        LayoutParams para = lLayout.getLayoutParams();
        if (metric.widthPixels == 1920) {
            para.width = 60 * 2;
            lLayout.setLayoutParams(para);
            viewWidth = metric.widthPixels - 60 * 2;
        } else if (metric.widthPixels == 1280) {
            para.width = 40 * 2;
            lLayout.setLayoutParams(para);
            viewWidth = metric.widthPixels - 40 * 2;
        }
        
        TableTitle title = TableViewUtil.getMajiangTableHor(this, viewWidth);
        
        // widthList = title.getmWidthList();
        widthList = new ArrayList<Float>();
        int i = 0;
        if (metric.widthPixels % 30 == 0) {// 1920
            for (i = 0; i < 46; i++) {
                if (i == 0) {
                    widthList.add((float) (viewWidth / 49 * 3 - 1));
                } else if (i == 25 || i == 24) {
                    widthList.add((float) (viewWidth / 49 * 1.5 + 18 - 1));
                } else {
                    widthList.add((float) (viewWidth / 49 - 1));
                }
            }
        } else if (metric.widthPixels % 32 == 0) {// 1280
            for (i = 0; i < 46; i++) {
                if (i == 0) {
                    widthList.add((float) (viewWidth / 49 * 3 - 1));
                } else if (i == 25 || i == 24) {
                    widthList.add((float) (viewWidth / 49 * 1.5 + 12 - 1));
                } else {
                    widthList.add((float) (viewWidth / 49 - 1));
                }
            }
        }
        mTableTopTitle = new TableFifFiveTitleView(this, widthList);
        LinearLayout.LayoutParams ll =
                new LinearLayout.LayoutParams(viewWidth, LayoutParams.WRAP_CONTENT);
        if (density < 1.8) {
            mTableTopTitle.addData(title, 1.8f);
        } else {
            mTableTopTitle.addData(title, density);
        }
        mTableTopTitle.setLayoutParams(ll);
        
        //
        // title.mWidthList=widthList;
        textSize = title.getDataTextSize();
        
        mTableBadyView = new TableBadyView(this, widthList);
        
        // itemHeight = textSize * 1.618f * density;
        itemHeight = widthList.get(3);
        int size = 50;
        int tableHeight = (int) (itemHeight * size + size + 1);
        
        LinearLayout.LayoutParams badyParams =
                new LinearLayout.LayoutParams(viewWidth, tableHeight);
        mTableBadyView.setLayoutParams(badyParams);
        
        // LayoutParams paras= mTopLayout.getLayoutParams();
        // paras.height = (int) (itemHeight*3);
        // mTopLayout.setLayoutParams(paras);
        mTopLayout.addView(mTableTopTitle);
        mContextLayout.addView(mTableBadyView, badyParams);
        
        mTableBottmBady = new TableFifFiveBottmBady(this);
        mTableBottmBady.setLayoutParams(ll);
        
        mBottomLayout.addView(mTableBottmBady);
        
        scrollview.setOnTouchListener(new OnTouchListener() {
            
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                
                // Logger.v("TEST", " scrollview " + arg1.getAction());
                return mGesture.onTouchEvent(arg1);
                // TODO Auto-generated method stub
            }
            
        });
        menuBtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                gotoMenuActivity();
            }
            
        });
    }
    
    @Override
    protected void updateView(List<LotteryInfo> lotteryInfo, String miss, String sum) {
        if (lotteryInfo.isEmpty()) {
            return;
        }
        ArrayList<ArrayList<ArrayList<String>>> data =
                new ArrayList<ArrayList<ArrayList<String>>>();
        int size = lotteryInfo.size();
        
        for (int index = 0; index < size; index++) {
            
            ArrayList<ArrayList<String>> itemList = new ArrayList<ArrayList<String>>();
            LotteryInfo info = lotteryInfo.get(index);
            String value = info.getLotteryNumbers();
            String[] array = value.split(",");
            int arraySize = array.length / 2;
            int[] all1_20 = new int[arraySize];
            int[] all21_40 = new int[arraySize];
            for (int j = 0; j < arraySize; j++) {
                all1_20[j] = Integer.valueOf(array[j]);
            }
            for (int j = arraySize; j < array.length; j++) {
                all21_40[j - arraySize] = Integer.valueOf(array[j]);
            }
            int[] aa = {all1_20[0], all1_20[1], all1_20[2]};
            if (isFirstData(info.getDateline())) {
                TableViewUtil.addCol(info.getIssue(), 4, itemList);// 1
            } else {
                TableViewUtil.addCol(info.getIssue(), 0, itemList);// 1
            }
            TableViewUtil.addCol(array[0], 2, itemList);// 2
            TableViewUtil.addCol(array[1], 2, itemList);// 3
            TableViewUtil.addCol(array[2], 2, itemList);// 4
            TableViewUtil.getDataArray(aa, all1_20, itemList, 20);
            
            TableViewUtil.addCol(getBigSmall(all1_20, 11), 3, itemList);// 27
            TableViewUtil.addCol(getjiou(all1_20), 3, itemList);// 28
            
            TableViewUtil.getDataArrayMajiang(all21_40, itemList, 21, 20);
            
            data.add(itemList);
        }
        mTableBadyView.updateData(data, widthList, TableViewUtil.redLine_majiang,
                (textSize + 2) * density, itemHeight);
        
        ArrayList<ArrayList<String>> bottomdata = new ArrayList<ArrayList<String>>();
        ArrayList<String> bottomitem = new ArrayList<String>();
        bottomitem.add("当前遗漏值");
        String[] missArray = miss.split(",");
        size = missArray.length;
        for (int index = 0; index < size; index++) {
            bottomitem.add(missArray[index]);
        }
        
        bottomdata.add(bottomitem);
        bottomitem = new ArrayList<String>();
        bottomitem.add("出现总次数");
        String[] sumArray = sum.split(",");
        size = sumArray.length;
        for (int index = 0; index < size; index++) {
            bottomitem.add(sumArray[index]);
        }
        bottomdata.add(bottomitem);
        ArrayList<Float> list = TableViewUtil.getBottomWidth(widthList, 4, 24, 25);
        mTableBottmBady.addData(bottomdata, TableViewUtil.redL_majiang_bo, list, textSize,
                R.drawable.icon_happy10_majiang, 21);
        
        scrollview.post(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                scrollview.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
        
        final MLinearLayout mLayout = (MLinearLayout) mBottomLayout;
        mLayout.reMeasure();
        
        LayoutParams lp = startLotto.getLayoutParams();
        lp.height = mLayout.getMeasuredHeight() / 2 - 1;
        startLotto.setLayoutParams(lp);
        
        lp = menuParent.getLayoutParams();
        lp.height = mLayout.getMeasuredHeight() / 2 - 2;
        menuParent.setLayoutParams(lp);
        
        System.out.println("bottom height : " + mLayout.getMeasuredHeight());
    }
    
    @Override
    public void updateTimeCount(String time) {
        if(!TextUtils.isEmpty(time)){
        	 startLotto.setText(time);
        }else{
        	 startLotto.setText(mCountdownTime);	
        }
       
    }
}
